<template>
	<view class="back-view" @touchmove.stop.prevent="moveHandle">
		<view class="view-background"></view>
		<view class="white-view-back">
			<view class="top-back">
				<view class="top-name">{{pageTitle}}</view>
				<view class="close-back" @click="closeBtnAction">
					<image class="close-img" src="../../../../static/close.png"></image>
				</view>
			</view>

			<view class="top-line"></view>
			<scroll-view class="center-back" scroll-y="true">
				<uni-forms ref="baseForm" :modelValue="baseFormData">
					<uni-forms-item label="手机号码：" labelWidth="100" labelAlign="right" required>
						<view class="item-back law-item-back">
							<input :disabled="editType == 'edit'" @input="onKeyInput" class="selectLaw"
								v-model="baseFormData.mobile" @focus="startEditAction" placeholder="请输入"></input>
							<view v-if="showMask" class="selectLaw-list">
								<scroll-view class="pop-content-back" scroll-y="true">
									<view class="pop-content-list" v-for="(item,index) in commUser"
										@click="clickPopIremAction(item)">
										<image class="pop-content-img" :src="item.account.avatarPic"></image>
										<view class="pop-content-name">{{item.account.nickName}}</view>
										<view class="pop-content-phone">{{item.account.mobile}}</view>
										<view class="pop-content-name">{{item.account.realName}}</view>
									</view>
								</scroll-view>
							</view>
						</view>
						<view class="mask-view" v-if="showMask" @tap="tapMask"></view>

					</uni-forms-item>

					<uni-forms-item label="律师姓名：" labelWidth="100" labelAlign="right" required>
						<view class="item-back">
							<uni-easyinput v-model="baseFormData.name" placeholder="请填写" />
						</view>
					</uni-forms-item>
					<uni-forms-item label="律师介绍：" labelWidth="100" labelAlign="right" required>
						<view class="item-back2 ">
							<myEditor :editorDetail='baseFormData.remark' @getContents="getContents"></myEditor>
						</view>
					</uni-forms-item>
				</uni-forms>
			</scroll-view>
			<view class="bottom-back">
				<view class="bottom-btn bottom-btn-cancel" @click="closeBtnAction">取消</view>
				<view class="bottom-btn" @click="requestAddData">确定</view>
			</view>
		</view>
	</view>
</template>

<script>
	import myEditor from "@/packageB/PC/editor/editor.vue"

	export default {
		components: {
			myEditor
		},
		props: {
			editType: {
				type: String,
				default: 'add',
			},
			item: {
				type: Object,
				default: {},
			},
		},
		data() {

			return {
				pageTitle: '添加律师',
				showMask: false,
				baseFormData: {
					'name': '',
					'mobile': '',
					'remark': '',
					'accId': '',
					'id': ''
				},

				commUser: [],
			}
		},
		onLoad() {

		},
		mounted() {
			if (this.editType == 'edit') {
				console.log(this.item)

				this.baseFormData.name = this.item.account.realName;
				this.baseFormData.mobile = this.item.account.mobile;
				this.baseFormData.id = this.item.id;
				this.baseFormData.remark = this.item.remark;
				console.log(this.baseFormData)
				this.pageTitle = '编辑律师';

			} else {
				this.requestListData();
			}
		},
		methods: {
			//新增律师时获取普通用户列表
			async requestListData() {
				let _this = this;
				let parmas = {

				}
				if (_this.baseFormData.mobile) {
					this.$set(parmas, 'mobile', 'lk:' + _this.baseFormData.mobile)
				}
				console.log(parmas)
				const res = await this.$myRequest({
					url: "/admin/commUser/selector/1",
					method: 'POST',
					data: parmas,
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					_this.commUser = res.data.data
				}
			},

			//编辑器赋值
			getContents(html) {
				this.baseFormData.remark = html;
				console.log(this.baseFormData.remark)
			},
			//保存网络请求
			async requestAddData() {
				let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/

				if (!this.baseFormData.mobile) {
					uni.showToast({
						title: '请填写手机号码',
						icon: 'none'
					})
					return;
				} else if (!reg.test(this.baseFormData.mobile)) { //判断手机号格式时候正确
					uni.showToast({
						title: '请填写正确的手机号',
						icon: 'none'
					})
					return
				}

				if (!this.baseFormData.name) {
					uni.showToast({
						title: '请填写律师姓名',
						icon: 'none'
					})
					return;
				}
				if (!this.baseFormData.remark || this.baseFormData.remark == '<p><br></p>') {
					uni.showToast({
						title: '请填写律师介绍',
						icon: 'none'
					})
					return;
				}
				let _this = this;
				console.log(this.baseFormData)
				let parmas = {
					'userId': _this.editType == 'edit' ? this.baseFormData.id : '',
					'name': this.baseFormData.name,
					'mobile': _this.editType == 'edit' ? "" : this.baseFormData.mobile,
					'remark': this.baseFormData.remark,
					'accId': _this.editType == 'add' ? this.baseFormData.accId : '',
				}
				console.log(parmas)
				const res = await this.$myRequest({
					url: "/admin/lawyer",
					method: _this.editType == 'edit' ? 'PUT' : 'POST',
					data: parmas,
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					uni.showToast({
						title: _this.editType == 'edit' ? '编辑成功' : '添加成功'
					})
					_this.$emit("successBack");
					_this.closeBtnAction()
				} else if (res.data.code == 400) {
					uni.showToast({
						icon: 'none',
						title: '手机号尚未注册'
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: res.data.msg
					})
				}
			},

			startEditAction(e) {
				console.log("开始编写")
				console.log(e)
				this.showMask = true
			},
			onKeyInput(event) {
				console.log(event.target.value)
				this.requestListData()
			},
			tapMask() {
				this.showMask = false
			},

			//选择工作组、律师
			clickPopIremAction(item) {
				this.baseFormData.name = item.account.realName;
				this.baseFormData.mobile = item.account.mobile;
				this.baseFormData.accId = item.accId;
				this.showMask = false
			},
			closeBtnAction() {
				this.$emit('addLawyerClose')
			},
			moveHandle() {
				return true
			},
		},
	}
</script>

<style>
	.back-view {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 199;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.view-background {
		position: absolute;
		width: 100%;
		height: 100%;
		z-index: 100;
		background-color: rgba(0, 0, 0, 0.3);
	}

	.white-view-back {
		width: 750px;
		height: 550px;
		padding: 24px;
		max-height: 750px;
		background-color: white;
		margin-top: -10px;
		border-radius: 4px;
		z-index: 1000;
		position: absolute;
	}

	.top-back {
		display: flex;
		justify-content: space-between;
	}

	.top-name {
		font-size: 18px;
		font-weight: normal;
		color: #333333;

	}

	.close-back {
		cursor: pointer;
		width: 20px;
		height: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.close-img {
		width: 12px;
		height: 12px;
	}

	.top-line {
		height: 1px;
		background: #EEEEEE;
		width: 100%;
		margin-top: 20px;
		margin-bottom: 20px;
	}

	.bottom-back {

		bottom: 0px;
		width: 100%;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		position: absolute;
		height: 60px;
		right: 16px;
	}

	.bottom-btn {
		cursor: pointer;
		width: 104px;
		height: 32px;
		background: #2DA9C5;
		border-radius: 2px;
		color: #FFFFFF;
		font-size: 14px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.bottom-btn-cancel {
		cursor: pointer;
		margin-right: 10px;
		background: #FFFFFF;
		border: 1px solid #EEEEEE;
		color: #333333;
	}

	.item-back {
		width: 450px;
	}

	.item-back2 {
		width: 600px;
		border: 1px solid #eee;
		border-radius: 5px;
	}

	.customer-item {
		display: flex;
		margin-top: 8px;
		align-items: center;

	}

	.customer-back {
		min-width: 168px;
		height: 44px;
		background: #E8F7FB;
		border-radius: 4px;
		display: flex;
		align-items: center;
		margin-right: 10px;
	}

	.customer-img {
		width: 20px;
		height: 20px;
		border-radius: 50%;
		margin-left: 8px;
	}

	.customer-name {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 2px;
	}

	.customer-phone {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 4px;
	}

	.customer-close {
		width: 16px;
		height: 16px;
		margin-right: 2px;
		margin-left: auto;
	}

	.customer-item-back {
		display: flex;
		flex-wrap: wrap;

	}

	.selectLaw {
		font-size: 14px;
		border: 1px solid #DCDFE6;
		border-radius: 4px;
		padding: 6px 10px;
		position: relative;

		flex-direction: row;
		align-items: center;
		z-index: 100000;
		background-color: white;
	}

	.law-item-back {
		background-color: white;
	}

	.selectLaw-list {
		margin-top: 2px;
		position: absolute;
		width: 400px;
		height: 180px;
		z-index: 100010;
		background-color: white;
		box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.2000);
		border-radius: 2px;
	}

	.mask-view {
		position: absolute;
		width: 100vw;
		height: 100vh;
		z-index: 9999;
		background-color: rgba(0, 0, 0, 0.0);
		top: -100px;
		left: 0;
		right: 0;
		bottom: 0;

	}



	.pop-content-back {
		margin-top: 10px;
		height: 160px;
		margin-left: 20px;
		width: 380px;
	}

	.pop-content-list {
		display: flex;
		align-items: center;
		margin-bottom: 10px;
		margin-top: 10px;
	}

	.pop-content-img {
		width: 20px;
		min-width: 20px;
		height: 20px;
		border-radius: 50%;
	}

	.pop-content-name {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 10px;
		min-width: 60px;
	}

	.pop-content-phone {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 10px;
	}

	.center-back {
		width: 100%;
		height: 450px;
	}
</style>
